//
//  ColorController.m
//  AppSettings
//
//  Created by 胡先剑 on 16/3/16.
//  Copyright © 2016年 胡先剑. All rights reserved.
//

#import "ColorController.h"
#import "ViewController.h"

@interface ColorController ()

@property (weak, nonatomic) IBOutlet UILabel *colorLabel;

@property (weak, nonatomic) IBOutlet UITextField *redTextField;
@property (weak, nonatomic) IBOutlet UITextField *greenTextField;
@property (weak, nonatomic) IBOutlet UITextField *blueTextField;
@property (weak, nonatomic) IBOutlet UISlider *redSilder;
@property (weak, nonatomic) IBOutlet UISlider *greenSilder;
@property (weak, nonatomic) IBOutlet UISlider *blueSilder;
@end

@implementation ColorController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self changeColor];
    
}

- (void)changeColor
{
    NSUserDefaults *d = [NSUserDefaults standardUserDefaults];
    float red = [d floatForKey:@"red"];
    float green = [d floatForKey:@"green"];
    float blue = [d floatForKey:@"blue"];
    self.redTextField.text = [NSString stringWithFormat:@"%.0f",red];
    self.greenTextField.text = [NSString stringWithFormat:@"%.0f",green];
    self.blueTextField.text = [NSString stringWithFormat:@"%.0f",blue];
    self.redSilder.value = red;
    self.greenSilder.value = green;
    self.blueSilder.value = blue;
    self.colorLabel.backgroundColor = [UIColor colorWithRed:red/255.0 green:green/255.0 blue:blue/255.0 alpha:1];
}

- (IBAction)backTop:(id)sender
{
    [self.view endEditing:YES];
    self.redSilder.value = [self.redTextField.text intValue];
    self.greenSilder.value = [self.greenTextField.text intValue];
    self.blueSilder.value = [self.blueTextField.text intValue];
    self.colorLabel.backgroundColor = [UIColor colorWithRed:self.redSilder.value/255.0 green:self.greenSilder.value/255.0 blue:self.blueSilder.value/255.0 alpha:1];
    
    NSUserDefaults *d = [NSUserDefaults standardUserDefaults];
    [d setFloat:self.redSilder.value forKey:@"red"];
    [d setFloat:self.greenSilder.value forKey:@"green"];
    [d setFloat:self.blueSilder.value forKey:@"blue"];
    [d synchronize];
}
- (IBAction)redSliderColor:(UISlider *)sender
{
    int value = sender.value;
    
    self.redTextField.text = [NSString stringWithFormat:@"%i",value];
    self.colorLabel.backgroundColor = [UIColor colorWithRed:value/255.0 green:[self.greenTextField.text intValue]/255.0 blue:[self.blueTextField.text intValue]/255.0 alpha:1];
    
    NSUserDefaults *d = [NSUserDefaults standardUserDefaults];
    [d setFloat:value forKey:@"red"];
    [d synchronize];
}

- (IBAction)greenSliderColor:(UISlider *)sender
{
    int value = sender.value;
    
    self.greenTextField.text = [NSString stringWithFormat:@"%i",value];
    self.colorLabel.backgroundColor = [UIColor colorWithRed:[self.redTextField.text intValue]/255.0 green:value/255.0 blue:[self.blueTextField.text intValue]/255.0 alpha:1];
    
    NSUserDefaults *d = [NSUserDefaults standardUserDefaults];
    [d setFloat:value forKey:@"green"];
    [d synchronize];
}

- (IBAction)blueSliderColor:(UISlider *)sender
{
    int value = sender.value;
    
    self.blueTextField.text = [NSString stringWithFormat:@"%i",value];
    self.colorLabel.backgroundColor = [UIColor colorWithRed:[self.redTextField.text intValue]/255.0 green:[self.greenTextField.text intValue]/255.0 blue:value/255.0 alpha:1];
    
    NSUserDefaults *d = [NSUserDefaults standardUserDefaults];
    [d setFloat:value forKey:@"blue"];
    [d synchronize];
}

- (IBAction)saveBtnPressed:(id)sender
{   
    [self dismissViewControllerAnimated:YES completion:nil];
}

@end
